package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.utils.FileUtil;
import com.uwlaser.utils.enums.DictValueEnum;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DataCourseParam;
import com.uwlaser.service.DataCourseParamService;
import com.uwlaser.service.dto.DataCourseParamDto;
import com.uwlaser.service.dto.DataCourseParamQueryParam;
import com.uwlaser.service.mapper.DataCourseParamMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DataCourseParamService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DataCourseParamServiceImpl extends CommonServiceImpl<DataCourseParamMapper, DataCourseParam> implements DataCourseParamService {

    // private final RedisUtils redisUtils;
    private final DataCourseParamMapper dataCourseParamMapper;

    @Override
    public PageInfo<DataCourseParamDto> queryAll(DataCourseParamQueryParam query, Pageable pageable) {
        IPage<DataCourseParam> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DataCourseParam> page = dataCourseParamMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DataCourseParamDto.class);
    }

    @Override
    public List<DataCourseParamDto> queryAll(DataCourseParamQueryParam query){
        return ConvertUtil.convertList(dataCourseParamMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DataCourseParamDto.class);
    }

    @Override
    public DataCourseParam getById(Integer id) {
        return dataCourseParamMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DataCourseParamDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DataCourseParamDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DataCourseParamDto resources) {
        DataCourseParam entity = ConvertUtil.convert(resources, DataCourseParam.class);
        return dataCourseParamMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DataCourseParamDto resources){
        DataCourseParam entity = ConvertUtil.convert(resources, DataCourseParam.class);
        int ret = dataCourseParamMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return dataCourseParamMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    @Override
    public void download(List<DataCourseParamDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DataCourseParamDto dataCourseParam : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品SN", dataCourseParam.getProductSn());
              map.put("产品型号", dataCourseParam.getProductCode());
              map.put("产品类型", dataCourseParam.getProductType());
              map.put("订单编号", dataCourseParam.getOrderCode());
              map.put("工单编号", dataCourseParam.getWorkOrderCode());
              map.put("产线编号", dataCourseParam.getLienCode());
              map.put("工段编号", dataCourseParam.getSectionCode());
              map.put("工位编号", dataCourseParam.getStationCode());
              map.put("工序编号", dataCourseParam.getOperationCode());
              map.put("设备编号", dataCourseParam.getDeviceCode());
              String result = dataCourseParam.getParamResult().equals(DictValueEnum.YES.getValue())
                      ? DictValueEnum.YES.getLabel()
                      : DictValueEnum.NO.getLabel();
              map.put("判断结果", result);
              map.put("参数编码", dataCourseParam.getParamCode());
              map.put("参数名称", dataCourseParam.getParamName());
              map.put("参数值", dataCourseParam.getParamValue());
              map.put("判断范围", dataCourseParam.getParamRange());
              map.put("参数单位", dataCourseParam.getParamUnit());
//              map.put("测试顺序", dataCourseParam.getParamNumber());
//              map.put("过站ID", dataCourseParam.getPassStationId());
//              map.put("创建人", dataCourseParam.getCreateBy());
              map.put("创建时间", dataCourseParam.getCreateTime());
//              map.put("更新人", dataCourseParam.getUpdateBy());
//              map.put("更新时间", dataCourseParam.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }
}
